<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【NodeJS】Node.js + Vue 实现 Excel 导出与保存 | Vixcity</title><meta name="keywords" content="Node,Vue,文件导出"><meta name="author" content="Vixcity"><meta name="copyright" content="Vixcity"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Node.js + Vue 实现 Excel 导出与保存我们的项目是前端用vue,服务端用node.js,这个excel导出我们已经用了一年,我们目前用到的无非是图片导出,文本导出,调调excel单元格距离等. 这个node端的封装是经过同事不断的review(感谢同事),俺不断的修改优化后的代码,当时也是我第一次接触node.js,只想告诉你,用它,稳稳的！ node.js服务端代码1.拿到需要">
<meta property="og:type" content="article">
<meta property="og:title" content="【NodeJS】Node.js + Vue 实现 Excel 导出与保存">
<meta property="og:url" content="http://vixcity.gitee.io/my-gitee-blog/2021/10/15/%E3%80%90NodeJS%E3%80%91Node.js%20+%20Vue%20%E5%AE%9E%E7%8E%B0%20Excel%20%E5%AF%BC%E5%87%BA%E4%B8%8E%E4%BF%9D%E5%AD%98/index.html">
<meta property="og:site_name" content="Vixcity">
<meta property="og:description" content="Node.js + Vue 实现 Excel 导出与保存我们的项目是前端用vue,服务端用node.js,这个excel导出我们已经用了一年,我们目前用到的无非是图片导出,文本导出,调调excel单元格距离等. 这个node端的封装是经过同事不断的review(感谢同事),俺不断的修改优化后的代码,当时也是我第一次接触node.js,只想告诉你,用它,稳稳的！ node.js服务端代码1.拿到需要">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://vixcity.gitee.io/my-gitee-blog/img/NodeJs.jpg">
<meta property="article:published_time" content="2021-10-14T16:00:00.000Z">
<meta property="article:modified_time" content="2022-01-04T03:04:47.324Z">
<meta property="article:author" content="Vixcity">
<meta property="article:tag" content="Node">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="文件导出">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://vixcity.gitee.io/my-gitee-blog/img/NodeJs.jpg"><link rel="shortcut icon" href="/my-gitee-blog/img/favicon.ico"><link rel="canonical" href="http://vixcity.gitee.io/my-gitee-blog/2021/10/15/%E3%80%90NodeJS%E3%80%91Node.js%20+%20Vue%20%E5%AE%9E%E7%8E%B0%20Excel%20%E5%AF%BC%E5%87%BA%E4%B8%8E%E4%BF%9D%E5%AD%98/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-gitee-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-gitee-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-gitee-blog/search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: '/my-gitee-blog/',
      css: '/my-gitee-blog/'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【NodeJS】Node.js + Vue 实现 Excel 导出与保存',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-01-04 11:04:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/my-gitee-blog/css/mycss.css"><link rel="stylesheet" href="/my-gitee-blog/css/newStyle.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome-animation@1.1.1/css/font-awesome-animation.css"><link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><link rel="stylesheet" href="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-gitee-blog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-gitee-blog/img/NodeJs.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-gitee-blog/">Vixcity</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【NodeJS】Node.js + Vue 实现 Excel 导出与保存</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-10-14T16:00:00.000Z" title="发表于 2021-10-15 00:00:00">2021-10-15</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-04T03:04:47.324Z" title="更新于 2022-01-04 11:04:47">2022-01-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/">大前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/Node/">Node</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>13分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【NodeJS】Node.js + Vue 实现 Excel 导出与保存"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="Node-js-Vue-实现-Excel-导出与保存"><a href="#Node-js-Vue-实现-Excel-导出与保存" class="headerlink" title="Node.js + Vue 实现 Excel 导出与保存"></a>Node.js + Vue 实现 Excel 导出与保存</h2><p>我们的项目是前端用vue,服务端用node.js,这个excel导出我们已经用了一年,我们目前用到的无非是图片导出,文本导出,调调excel单元格距离等.</p>
<p>这个node端的封装是经过同事不断的review(感谢同事),俺不断的修改优化后的代码,当时也是我第一次接触node.js,只想告诉你,用它,稳稳的！</p>
<h3 id="node-js服务端代码"><a href="#node-js服务端代码" class="headerlink" title="node.js服务端代码"></a>node.js服务端代码</h3><h4 id="1-拿到需要导出的数据之后如何使用-假数据模拟-下面是页面"><a href="#1-拿到需要导出的数据之后如何使用-假数据模拟-下面是页面" class="headerlink" title="1.拿到需要导出的数据之后如何使用(假数据模拟,下面是页面)"></a>1.拿到需要导出的数据之后如何使用(假数据模拟,下面是页面)</h4><p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110150835680.webp" alt="图片"></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"> <span class="function"><span class="title">constructor</span>(<span class="params">prop</span>)</span> &#123; </span><br><span class="line">   <span class="built_in">super</span>(prop)    <span class="comment">// 定义excel头部数据    </span></span><br><span class="line">  <span class="built_in">this</span>.header = [</span><br><span class="line">   &#123;</span><br><span class="line">     <span class="attr">header</span>: <span class="string">&#x27;游戏&#x27;</span>, </span><br><span class="line">     <span class="attr">key</span>: <span class="string">&#x27;gameInfo&#x27;</span>,</span><br><span class="line">     <span class="attr">width</span>: <span class="number">30</span></span><br><span class="line">   &#125;, </span><br><span class="line">   &#123;</span><br><span class="line">     <span class="attr">header</span>: <span class="string">&#x27;宣传图片&#x27;</span>,</span><br><span class="line">     <span class="attr">key</span>: <span class="string">&#x27;image&#x27;</span>,</span><br><span class="line">     <span class="attr">width</span>: <span class="number">15</span> </span><br><span class="line">   &#125;,  </span><br><span class="line">   &#123;</span><br><span class="line">     <span class="attr">header</span>: <span class="string">&#x27;游戏详情页&#x27;</span>, </span><br><span class="line">     <span class="attr">key</span>: <span class="string">&#x27;path&#x27;</span>,</span><br><span class="line">     <span class="attr">width</span>: <span class="number">15</span> </span><br><span class="line">   &#125;,</span><br><span class="line">   &#123;</span><br><span class="line">     <span class="attr">header</span>: <span class="string">&#x27;状态&#x27;</span>,</span><br><span class="line">     <span class="attr">key</span>: <span class="string">&#x27;activeStatus&#x27;</span>,</span><br><span class="line">     <span class="attr">width</span>: <span class="number">30</span></span><br><span class="line">    &#125;, </span><br><span class="line">    &#123;</span><br><span class="line">       <span class="attr">header</span>: <span class="string">&#x27;排序权重&#x27;</span>, </span><br><span class="line">       <span class="attr">key</span>: <span class="string">&#x27;sort&#x27;</span>, </span><br><span class="line">       <span class="attr">width</span>: <span class="number">30</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">header</span>: <span class="string">&#x27;最近编辑时间&#x27;</span>,</span><br><span class="line">      <span class="attr">key</span>: <span class="string">&#x27;updateTime&#x27;</span>,</span><br><span class="line">      <span class="attr">width</span>: <span class="number">30</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">header</span>: <span class="string">&#x27;最近编辑人&#x27;</span>, </span><br><span class="line">      <span class="attr">key</span>: <span class="string">&#x27;operatorName&#x27;</span>,</span><br><span class="line">      <span class="attr">width</span>: <span class="number">30</span></span><br><span class="line">    &#125;,</span><br><span class="line">  ]</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**   * 导出游戏管理数据   */</span>  </span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="title">exportGameEndGameManage</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line"> <span class="keyword">const</span> &#123; list &#125; = <span class="keyword">await</span> <span class="built_in">this</span>.commonGameEndGameManage(<span class="literal">true</span>)</span><br><span class="line"> <span class="built_in">console</span>.log(list, <span class="string">&#x27;list&#x27;</span>)</span><br><span class="line"> <span class="keyword">const</span> baseExcelInfo = &#123;</span><br><span class="line">  <span class="attr">data</span>: list,</span><br><span class="line">  <span class="attr">filename</span>: <span class="string">&#x27;gameManageList&#x27;</span>,</span><br><span class="line">  <span class="attr">header</span>: <span class="built_in">this</span>.header,</span><br><span class="line">  <span class="attr">sheetName</span>: <span class="string">&#x27;游戏管理列表&#x27;</span>,</span><br><span class="line">  <span class="attr">imageKeys</span>: [</span><br><span class="line">   &#123;</span><br><span class="line">     <span class="attr">name</span>: <span class="string">&#x27;image&#x27;</span>, </span><br><span class="line">     <span class="attr">imgWidth</span>: <span class="string">&#x27;100&#x27;</span>,</span><br><span class="line">     <span class="attr">imgHeight</span>: <span class="string">&#x27;100&#x27;</span>,</span><br><span class="line">   &#125;,</span><br><span class="line">  ],</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">await</span> <span class="built_in">this</span>.service.common.exportFile.exportExcel(baseExcelInfo)  &#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>list就是拿到的数据,打印如下<br>baseExcelInfo用来定义基本的参数<br>data 表示的是excel的数据来源<br>filename 是文件名(但是前端的excel导出会进行覆盖)<br>header表示的是表格的头部<br>sheetName表示的是excel的表名<br>imageKeys:图片的信息:字段名称，图片的宽高,但是只要有图片,name必须设</p>
</blockquote>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110150840342.webp" alt="图片"></p>
<p><code>很重要的一点就是,假设从表里面的数据返回的status是1,那么我肯定导出的不能1,应该是对应的一个中文,所以在导出前,应该进行处理,这个处理应该是在服务端来做,而不是前端做一遍,然后为了导出这个功能重新做一遍</code>举个例子</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  公共游戏管理数据</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123; Boolean &#125;</span> </span>isExport 是否导出</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">async</span> <span class="function"><span class="title">commonGameEndGameManage</span>(<span class="params">isExport</span>)</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> activeStatus = &#123; <span class="comment">// 这个按道理写在constructor里面哈</span></span><br><span class="line">   <span class="number">1</span>: <span class="string">&#x27;打开&#x27;</span>,</span><br><span class="line">   <span class="number">2</span>: <span class="string">&#x27;关闭&#x27;</span>,</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> &#123; ctx, app &#125; = <span class="built_in">this</span></span><br><span class="line">  <span class="keyword">const</span> &#123; limit, offset &#125; = <span class="built_in">this</span>.paginationDeal(ctx.request.query)</span><br><span class="line">  <span class="keyword">const</span> isPagi = isExport ? &#123;&#125; : &#123; limit, offset &#125;</span><br><span class="line">  <span class="keyword">const</span> &#123; list, total &#125; = <span class="keyword">await</span> ctx.service.operateManage.gameEndPage.</span><br><span class="line"></span><br><span class="line">  getGameEndGameManage(&#123; isPagi &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> data = list.map(<span class="function"><span class="params">node</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; status, ...params &#125; = node.toJSON()</span><br><span class="line">    <span class="keyword">const</span> activeStatus = activeStatus[status]</span><br><span class="line">    <span class="keyword">return</span> &#123; activeStatus, status, ...params &#125;</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> &#123; <span class="attr">list</span>: data, total &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-exportExcel的封装"><a href="#2-exportExcel的封装" class="headerlink" title="2.exportExcel的封装"></a>2.exportExcel的封装</h4><blockquote>
<p>首先安装对应的包 npm install exceljs –save<br>然后复制下面的代码就好了</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&#x27;use strict&#x27;</span></span><br><span class="line"><span class="keyword">const</span> Service = <span class="built_in">require</span>(<span class="string">&#x27;egg&#x27;</span>).Service</span><br><span class="line"><span class="comment">// 引入exceljs</span></span><br><span class="line"><span class="keyword">const</span> Excel = <span class="built_in">require</span>(<span class="string">&#x27;exceljs&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 导出文件相关服务</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">exportFileService</span> <span class="keyword">extends</span> <span class="title">Service</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">prop</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(prop)</span><br><span class="line">    <span class="built_in">this</span>.defaultViews = [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">x</span>: <span class="number">0</span>,</span><br><span class="line">        <span class="attr">y</span>: <span class="number">0</span>,</span><br><span class="line">        <span class="attr">width</span>: <span class="number">10000</span>,</span><br><span class="line">        <span class="attr">height</span>: <span class="number">20000</span>,</span><br><span class="line">        <span class="attr">firstSheet</span>: <span class="number">0</span>,</span><br><span class="line">        <span class="attr">activeTab</span>: <span class="number">1</span>,</span><br><span class="line">        <span class="attr">visibility</span>: <span class="string">&#x27;visible&#x27;</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    ]</span><br><span class="line">    <span class="built_in">this</span>.fontName = <span class="string">&#x27;Arial Unicode MS&#x27;</span></span><br><span class="line">    <span class="built_in">this</span>.font = &#123; <span class="attr">name</span>: <span class="built_in">this</span>.fontName, <span class="attr">family</span>: <span class="number">4</span>, <span class="attr">size</span>: <span class="number">13</span> &#125;</span><br><span class="line">    <span class="built_in">this</span>.fill = &#123; <span class="attr">type</span>: <span class="string">&#x27;pattern&#x27;</span>, <span class="attr">pattern</span>: <span class="string">&#x27;solid&#x27;</span>, <span class="attr">fgColor</span>: &#123; <span class="attr">argb</span>: <span class="string">&#x27;FF8DB4E2&#x27;</span> &#125; &#125;</span><br><span class="line">    <span class="built_in">this</span>.border = &#123; <span class="attr">style</span>: <span class="string">&#x27;thin&#x27;</span>, <span class="attr">color</span>: &#123; <span class="attr">argb</span>: <span class="string">&#x27;cccccc&#x27;</span> &#125; &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 导出excel</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; Object &#125;</span> </span>config 传入的excel对象</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; Array &#125;</span> </span>config.data excel的数据</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.filename excel文件名</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; Array &#125;</span> </span>config.header excel的头部</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.sheetName 表名</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; Array &#125;</span> </span>config.imageKeys 需要转化图片的key</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.creator 创建表的人</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.lastModifiedBy 最后修改表的人</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.imageKeys.imgWidth 图片的宽度</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.imageKeys.imgHeight 图片的高度</span></span><br><span class="line"><span class="comment">   * */</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">exportExcel</span>(<span class="params">&#123;</span></span></span><br><span class="line"><span class="params"><span class="function">    data = [],</span></span></span><br><span class="line"><span class="params"><span class="function">    filename = <span class="string">&#x27;file&#x27;</span>,</span></span></span><br><span class="line"><span class="params"><span class="function">    header,</span></span></span><br><span class="line"><span class="params"><span class="function">    sheetName = <span class="string">&#x27;sheet1&#x27;</span>,</span></span></span><br><span class="line"><span class="params"><span class="function">    imageKeys = [],</span></span></span><br><span class="line"><span class="params"><span class="function">    creator = <span class="string">&#x27;me&#x27;</span>,</span></span></span><br><span class="line"><span class="params"><span class="function">    lastModifiedBy = <span class="string">&#x27;her&#x27;</span>,</span></span></span><br><span class="line"><span class="params"><span class="function">  &#125;</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span></span><br><span class="line">    <span class="keyword">const</span> workbook = <span class="keyword">new</span> Excel.Workbook()</span><br><span class="line">    <span class="comment">// 设置属性 -创建着以及最后修改的人</span></span><br><span class="line">    workbook.creator = creator</span><br><span class="line">    workbook.lastModifiedBy = lastModifiedBy</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 时间获取一次就好</span></span><br><span class="line">    <span class="keyword">const</span> now = <span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line">    workbook.created = now</span><br><span class="line">    workbook.modified = now</span><br><span class="line">    workbook.lastPrinted = now</span><br><span class="line">    <span class="keyword">const</span> worksheet = workbook.addWorksheet(sheetName)</span><br><span class="line">    <span class="comment">// 设置打开时候的视图-设置位置</span></span><br><span class="line">    workbook.views = <span class="built_in">this</span>.defaultViews</span><br><span class="line">    <span class="comment">// 使工作表可见</span></span><br><span class="line">    worksheet.state = <span class="string">&#x27;visible&#x27;</span></span><br><span class="line">    worksheet.columns = header</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">1</span>; i &lt;= header.length; i++) &#123;</span><br><span class="line">      worksheet.getColumn(i).alignment = &#123; <span class="attr">vertical</span>: <span class="string">&#x27;middle&#x27;</span>, <span class="attr">horizontal</span>: <span class="string">&#x27;center&#x27;</span> &#125;</span><br><span class="line">      worksheet.getColumn(i).font = &#123; <span class="attr">name</span>: <span class="string">&#x27;Arial Unicode MS&#x27;</span> &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    worksheet.addRows(data)</span><br><span class="line">    <span class="comment">// 处理图片</span></span><br><span class="line">    <span class="keyword">const</span> imageList = <span class="built_in">this</span>.getImageList(imageKeys, data, header)</span><br><span class="line">    <span class="comment">// 添加图片到sheet</span></span><br><span class="line">    <span class="keyword">await</span> <span class="built_in">this</span>.addPicToSheet(imageList, imageKeys, workbook, worksheet)</span><br><span class="line">    <span class="comment">// 多级表头</span></span><br><span class="line">    <span class="keyword">const</span> headerOPtion = header.filter(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (item.type &amp;&amp; item.type === <span class="string">&#x27;multi&#x27;</span>) &#123;</span><br><span class="line">        header.splice(index, <span class="number">1</span>)</span><br><span class="line">        <span class="keyword">return</span> item</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> item.type &amp;&amp; item.type === <span class="string">&#x27;multi&#x27;</span></span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// 多级表头重置设置表头</span></span><br><span class="line">    <span class="keyword">if</span> (headerOPtion.length) &#123;</span><br><span class="line">      headerOPtion[<span class="number">0</span>].headerText.forEach(<span class="function">(<span class="params">text, index</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> borderAttr = &#123; <span class="attr">top</span>: <span class="built_in">this</span>.border, <span class="attr">left</span>: </span><br><span class="line">        <span class="built_in">this</span>.border, <span class="attr">bottom</span>: <span class="built_in">this</span>.border, <span class="attr">right</span>: <span class="built_in">this</span>.border, index &#125;</span><br><span class="line">        <span class="keyword">const</span> headerAttr = [</span><br><span class="line">          &#123;</span><br><span class="line">            <span class="attr">attr</span>: <span class="string">&#x27;values&#x27;</span>,</span><br><span class="line">            <span class="attr">value</span>: text,</span><br><span class="line">          &#125;,</span><br><span class="line">          &#123;</span><br><span class="line">            <span class="attr">attr</span>: <span class="string">&#x27;font&#x27;</span>,</span><br><span class="line">            <span class="attr">value</span>: <span class="built_in">this</span>.font,</span><br><span class="line">          &#125;,</span><br><span class="line">          &#123;</span><br><span class="line">            <span class="attr">attr</span>: <span class="string">&#x27;fill&#x27;</span>,</span><br><span class="line">            <span class="attr">value</span>: <span class="built_in">this</span>.fill,</span><br><span class="line">          &#125;,</span><br><span class="line">          &#123;</span><br><span class="line">            <span class="attr">attr</span>: <span class="string">&#x27;border&#x27;</span>,</span><br><span class="line">            <span class="attr">value</span>: borderAttr,</span><br><span class="line">          &#125;,</span><br><span class="line">        ]</span><br><span class="line">        headerAttr.map(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">          worksheet.getRow(index + <span class="number">1</span>)[item.attr] = item.value</span><br><span class="line">          <span class="keyword">return</span> worksheet</span><br><span class="line">        &#125;)</span><br><span class="line">      &#125;)</span><br><span class="line">      headerOPtion[<span class="number">0</span>].mergeOption.forEach(<span class="function"><span class="params">merge</span> =&gt;</span> &#123;</span><br><span class="line">        worksheet.mergeCells(merge)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 设置表头样式</span></span><br><span class="line">      worksheet.getRow(<span class="number">1</span>).font = <span class="built_in">this</span>.font</span><br><span class="line">      worksheet.getRow(<span class="number">1</span>).fill = <span class="built_in">this</span>.fill</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> bufferContent = <span class="keyword">await</span> workbook.xlsx.writeBuffer()</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 设置</span></span><br><span class="line">    ctx.set(<span class="string">&#x27;Content-disposition&#x27;</span>, <span class="string">`attachment;filename=<span class="subst">$&#123;filename&#125;</span>.xlsx`</span>)</span><br><span class="line">    <span class="comment">// 返回文件buffer</span></span><br><span class="line">    ctx.body = bufferContent</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 设置图片大小</span></span><br><span class="line">  <span class="function"><span class="title">getImageList</span>(<span class="params">imageKeys, data, header</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> imageKeys.map(</span><br><span class="line">      <span class="function"><span class="params">key</span> =&gt;</span> data.map(</span><br><span class="line">        <span class="function">(<span class="params">item, index</span>) =&gt;</span> (&#123;</span><br><span class="line">          key,</span><br><span class="line">          <span class="attr">url</span>: item[key.name],</span><br><span class="line">          <span class="attr">col</span>: <span class="built_in">this</span>.app.utils.index.getIndexByKey(header, key.name) + <span class="number">1</span>,</span><br><span class="line">          <span class="attr">row</span>: index + <span class="number">2</span>,</span><br><span class="line">          <span class="attr">width</span>: key.imgWidth,</span><br><span class="line">          <span class="attr">height</span>: key.imgHeight,</span><br><span class="line">        &#125;)</span><br><span class="line">      )</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 添加图片到sheet</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">addPicToSheet</span>(<span class="params">imageList, imageKeys, workbook, worksheet</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (imageKeys.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">      <span class="keyword">await</span> <span class="built_in">Promise</span>.all(imageList.map(<span class="keyword">async</span> imgArr =&gt; &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">await</span> <span class="built_in">Promise</span>.all(imgArr.map(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">          <span class="keyword">const</span> &#123; url, width, height, row, col &#125; = item</span><br><span class="line">          <span class="comment">// 因为有的图片是不存在的需要判断</span></span><br><span class="line">          <span class="keyword">if</span> (url) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.app.utils.index.getBase64(url, <span class="built_in">this</span>.ctx).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">              <span class="keyword">if</span> (!url) <span class="keyword">return</span></span><br><span class="line">              <span class="keyword">const</span> imgType = url.split(<span class="string">&#x27;?&#x27;</span>)[<span class="number">0</span>].substring(url.split(<span class="string">&#x27;?&#x27;</span>)[<span class="number">0</span>].</span><br><span class="line">              lastIndexOf(<span class="string">&#x27;.&#x27;</span>) + <span class="number">1</span>).toLowerCase()</span><br><span class="line">              <span class="keyword">const</span> id = workbook.addImage(&#123;</span><br><span class="line">                <span class="attr">base64</span>: res,</span><br><span class="line">                <span class="attr">extension</span>: imgType,</span><br><span class="line">              &#125;)</span><br><span class="line">              worksheet.addImage(id, &#123;</span><br><span class="line">                <span class="attr">tl</span>: &#123; <span class="attr">col</span>: col - <span class="number">1</span>, <span class="attr">row</span>: row - <span class="number">1</span> &#125;,</span><br><span class="line">                <span class="attr">ext</span>: &#123; width, height &#125;,</span><br><span class="line">              &#125;)</span><br><span class="line">              worksheet.getRow(row).height = height</span><br><span class="line">              <span class="comment">// // 去掉背景链接</span></span><br><span class="line">              worksheet.getRow(row).getCell(item.key.name).value = <span class="string">&#x27;&#x27;</span></span><br><span class="line">            &#125;)</span><br><span class="line">          &#125;</span><br><span class="line">          <span class="keyword">return</span> item</span><br><span class="line">        &#125;))</span><br><span class="line">      &#125;))</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = exportFileService</span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span></span><br><span class="line"><span class="keyword">const</span> Service = <span class="built_in">require</span>(<span class="string">&#x27;egg&#x27;</span>).Service</span><br><span class="line"><span class="comment">// 引入exceljs</span></span><br><span class="line"><span class="keyword">const</span> Excel = <span class="built_in">require</span>(<span class="string">&#x27;exceljs&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 导出文件相关服务</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">exportFileService</span> <span class="keyword">extends</span> <span class="title">Service</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">prop</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(prop)</span><br><span class="line">    <span class="built_in">this</span>.defaultViews = [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">x</span>: <span class="number">0</span>,</span><br><span class="line">        <span class="attr">y</span>: <span class="number">0</span>,</span><br><span class="line">        <span class="attr">width</span>: <span class="number">10000</span>,</span><br><span class="line">        <span class="attr">height</span>: <span class="number">20000</span>,</span><br><span class="line">        <span class="attr">firstSheet</span>: <span class="number">0</span>,</span><br><span class="line">        <span class="attr">activeTab</span>: <span class="number">1</span>,</span><br><span class="line">        <span class="attr">visibility</span>: <span class="string">&#x27;visible&#x27;</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    ]</span><br><span class="line">    <span class="built_in">this</span>.fontName = <span class="string">&#x27;Arial Unicode MS&#x27;</span></span><br><span class="line">    <span class="built_in">this</span>.font = &#123; <span class="attr">name</span>: <span class="built_in">this</span>.fontName, <span class="attr">family</span>: <span class="number">4</span>, <span class="attr">size</span>: <span class="number">13</span> &#125;</span><br><span class="line">    <span class="built_in">this</span>.fill = &#123; <span class="attr">type</span>: <span class="string">&#x27;pattern&#x27;</span>, <span class="attr">pattern</span>: <span class="string">&#x27;solid&#x27;</span>, <span class="attr">fgColor</span>: &#123; <span class="attr">argb</span>: <span class="string">&#x27;FF8DB4E2&#x27;</span> &#125; &#125;</span><br><span class="line">    <span class="built_in">this</span>.border = &#123; <span class="attr">style</span>: <span class="string">&#x27;thin&#x27;</span>, <span class="attr">color</span>: &#123; <span class="attr">argb</span>: <span class="string">&#x27;cccccc&#x27;</span> &#125; &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 导出excel</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; Object &#125;</span> </span>config 传入的excel对象</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; Array &#125;</span> </span>config.data excel的数据</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.filename excel文件名</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; Array &#125;</span> </span>config.header excel的头部</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.sheetName 表名</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; Array &#125;</span> </span>config.imageKeys 需要转化图片的key</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.creator 创建表的人</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.lastModifiedBy 最后修改表的人</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.imageKeys.imgWidth 图片的宽度</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>config.imageKeys.imgHeight 图片的高度</span></span><br><span class="line"><span class="comment">   * */</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">exportExcel</span>(<span class="params">&#123;</span></span></span><br><span class="line"><span class="params"><span class="function">    data = [],</span></span></span><br><span class="line"><span class="params"><span class="function">    filename = <span class="string">&#x27;file&#x27;</span>,</span></span></span><br><span class="line"><span class="params"><span class="function">    header,</span></span></span><br><span class="line"><span class="params"><span class="function">    sheetName = <span class="string">&#x27;sheet1&#x27;</span>,</span></span></span><br><span class="line"><span class="params"><span class="function">    imageKeys = [],</span></span></span><br><span class="line"><span class="params"><span class="function">    creator = <span class="string">&#x27;me&#x27;</span>,</span></span></span><br><span class="line"><span class="params"><span class="function">    lastModifiedBy = <span class="string">&#x27;her&#x27;</span>,</span></span></span><br><span class="line"><span class="params"><span class="function">  &#125;</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span></span><br><span class="line">    <span class="keyword">const</span> workbook = <span class="keyword">new</span> Excel.Workbook()</span><br><span class="line">    <span class="comment">// 设置属性 -创建着以及最后修改的人</span></span><br><span class="line">    workbook.creator = creator</span><br><span class="line">    workbook.lastModifiedBy = lastModifiedBy</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 时间获取一次就好</span></span><br><span class="line">    <span class="keyword">const</span> now = <span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line">    workbook.created = now</span><br><span class="line">    workbook.modified = now</span><br><span class="line">    workbook.lastPrinted = now</span><br><span class="line">    <span class="keyword">const</span> worksheet = workbook.addWorksheet(sheetName)</span><br><span class="line">    <span class="comment">// 设置打开时候的视图-设置位置</span></span><br><span class="line">    workbook.views = <span class="built_in">this</span>.defaultViews</span><br><span class="line">    <span class="comment">// 使工作表可见</span></span><br><span class="line">    worksheet.state = <span class="string">&#x27;visible&#x27;</span></span><br><span class="line">    worksheet.columns = header</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">1</span>; i &lt;= header.length; i++) &#123;</span><br><span class="line">      worksheet.getColumn(i).alignment = &#123; <span class="attr">vertical</span>: <span class="string">&#x27;middle&#x27;</span>, <span class="attr">horizontal</span>: <span class="string">&#x27;center&#x27;</span> &#125;</span><br><span class="line">      worksheet.getColumn(i).font = &#123; <span class="attr">name</span>: <span class="string">&#x27;Arial Unicode MS&#x27;</span> &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    worksheet.addRows(data)</span><br><span class="line">    <span class="comment">// 处理图片</span></span><br><span class="line">    <span class="keyword">const</span> imageList = <span class="built_in">this</span>.getImageList(imageKeys, data, header)</span><br><span class="line">    <span class="comment">// 添加图片到sheet</span></span><br><span class="line">    <span class="keyword">await</span> <span class="built_in">this</span>.addPicToSheet(imageList, imageKeys, workbook, worksheet)</span><br><span class="line">    <span class="comment">// 多级表头</span></span><br><span class="line">    <span class="keyword">const</span> headerOPtion = header.filter(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (item.type &amp;&amp; item.type === <span class="string">&#x27;multi&#x27;</span>) &#123;</span><br><span class="line">        header.splice(index, <span class="number">1</span>)</span><br><span class="line">        <span class="keyword">return</span> item</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> item.type &amp;&amp; item.type === <span class="string">&#x27;multi&#x27;</span></span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// 多级表头重置设置表头</span></span><br><span class="line">    <span class="keyword">if</span> (headerOPtion.length) &#123;</span><br><span class="line">      headerOPtion[<span class="number">0</span>].headerText.forEach(<span class="function">(<span class="params">text, index</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> borderAttr = &#123; <span class="attr">top</span>: <span class="built_in">this</span>.border, <span class="attr">left</span>: </span><br><span class="line">        <span class="built_in">this</span>.border, <span class="attr">bottom</span>: <span class="built_in">this</span>.border, <span class="attr">right</span>: <span class="built_in">this</span>.border, index &#125;</span><br><span class="line">        <span class="keyword">const</span> headerAttr = [</span><br><span class="line">          &#123;</span><br><span class="line">            <span class="attr">attr</span>: <span class="string">&#x27;values&#x27;</span>,</span><br><span class="line">            <span class="attr">value</span>: text,</span><br><span class="line">          &#125;,</span><br><span class="line">          &#123;</span><br><span class="line">            <span class="attr">attr</span>: <span class="string">&#x27;font&#x27;</span>,</span><br><span class="line">            <span class="attr">value</span>: <span class="built_in">this</span>.font,</span><br><span class="line">          &#125;,</span><br><span class="line">          &#123;</span><br><span class="line">            <span class="attr">attr</span>: <span class="string">&#x27;fill&#x27;</span>,</span><br><span class="line">            <span class="attr">value</span>: <span class="built_in">this</span>.fill,</span><br><span class="line">          &#125;,</span><br><span class="line">          &#123;</span><br><span class="line">            <span class="attr">attr</span>: <span class="string">&#x27;border&#x27;</span>,</span><br><span class="line">            <span class="attr">value</span>: borderAttr,</span><br><span class="line">          &#125;,</span><br><span class="line">        ]</span><br><span class="line">        headerAttr.map(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">          worksheet.getRow(index + <span class="number">1</span>)[item.attr] = item.value</span><br><span class="line">          <span class="keyword">return</span> worksheet</span><br><span class="line">        &#125;)</span><br><span class="line">      &#125;)</span><br><span class="line">      headerOPtion[<span class="number">0</span>].mergeOption.forEach(<span class="function"><span class="params">merge</span> =&gt;</span> &#123;</span><br><span class="line">        worksheet.mergeCells(merge)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 设置表头样式</span></span><br><span class="line">      worksheet.getRow(<span class="number">1</span>).font = <span class="built_in">this</span>.font</span><br><span class="line">      worksheet.getRow(<span class="number">1</span>).fill = <span class="built_in">this</span>.fill</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> bufferContent = <span class="keyword">await</span> workbook.xlsx.writeBuffer()</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 设置</span></span><br><span class="line">    ctx.set(<span class="string">&#x27;Content-disposition&#x27;</span>, <span class="string">`attachment;filename=<span class="subst">$&#123;filename&#125;</span>.xlsx`</span>)</span><br><span class="line">    <span class="comment">// 返回文件buffer</span></span><br><span class="line">    ctx.body = bufferContent</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 设置图片大小</span></span><br><span class="line">  <span class="function"><span class="title">getImageList</span>(<span class="params">imageKeys, data, header</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> imageKeys.map(</span><br><span class="line">      <span class="function"><span class="params">key</span> =&gt;</span> data.map(</span><br><span class="line">        <span class="function">(<span class="params">item, index</span>) =&gt;</span> (&#123;</span><br><span class="line">          key,</span><br><span class="line">          <span class="attr">url</span>: item[key.name],</span><br><span class="line">          <span class="attr">col</span>: <span class="built_in">this</span>.app.utils.index.getIndexByKey(header, key.name) + <span class="number">1</span>,</span><br><span class="line">          <span class="attr">row</span>: index + <span class="number">2</span>,</span><br><span class="line">          <span class="attr">width</span>: key.imgWidth,</span><br><span class="line">          <span class="attr">height</span>: key.imgHeight,</span><br><span class="line">        &#125;)</span><br><span class="line">      )</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 添加图片到sheet</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">addPicToSheet</span>(<span class="params">imageList, imageKeys, workbook, worksheet</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (imageKeys.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">      <span class="keyword">await</span> <span class="built_in">Promise</span>.all(imageList.map(<span class="keyword">async</span> imgArr =&gt; &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">await</span> <span class="built_in">Promise</span>.all(imgArr.map(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">          <span class="keyword">const</span> &#123; url, width, height, row, col &#125; = item</span><br><span class="line">          <span class="comment">// 因为有的图片是不存在的需要判断</span></span><br><span class="line">          <span class="keyword">if</span> (url) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.app.utils.index.getBase64(url, <span class="built_in">this</span>.ctx).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">              <span class="keyword">if</span> (!url) <span class="keyword">return</span></span><br><span class="line">              <span class="keyword">const</span> imgType = url.split(<span class="string">&#x27;?&#x27;</span>)[<span class="number">0</span>].substring(url.split(<span class="string">&#x27;?&#x27;</span>)[<span class="number">0</span>].</span><br><span class="line">              lastIndexOf(<span class="string">&#x27;.&#x27;</span>) + <span class="number">1</span>).toLowerCase()</span><br><span class="line">              <span class="keyword">const</span> id = workbook.addImage(&#123;</span><br><span class="line">                <span class="attr">base64</span>: res,</span><br><span class="line">                <span class="attr">extension</span>: imgType,</span><br><span class="line">              &#125;)</span><br><span class="line">              worksheet.addImage(id, &#123;</span><br><span class="line">                <span class="attr">tl</span>: &#123; <span class="attr">col</span>: col - <span class="number">1</span>, <span class="attr">row</span>: row - <span class="number">1</span> &#125;,</span><br><span class="line">                <span class="attr">ext</span>: &#123; width, height &#125;,</span><br><span class="line">              &#125;)</span><br><span class="line">              worksheet.getRow(row).height = height</span><br><span class="line">              <span class="comment">// // 去掉背景链接</span></span><br><span class="line">              worksheet.getRow(row).getCell(item.key.name).value = <span class="string">&#x27;&#x27;</span></span><br><span class="line">            &#125;)</span><br><span class="line">          &#125;</span><br><span class="line">          <span class="keyword">return</span> item</span><br><span class="line">        &#125;))</span><br><span class="line">      &#125;))</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = exportFileService</span><br></pre></td></tr></table></figure>

<h4 id="3-调用下载接口后node-js返回的信息"><a href="#3-调用下载接口后node-js返回的信息" class="headerlink" title="3.调用下载接口后node.js返回的信息"></a>3.调用下载接口后node.js返回的信息</h4><blockquote>
<p>前端看到的就是一个二进制文件流</p>
</blockquote>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110150844115.webp" alt="图片"></p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110150844585.webp" alt="图片"></p>
<p>推荐了解 content-type: application/octet-stream</p>
<h3 id="前端代码"><a href="#前端代码" class="headerlink" title="前端代码"></a>前端代码</h3><p>接口</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 文件导出</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">exportFile</span>(<span class="params">url, params</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> request(&#123;</span><br><span class="line">    <span class="attr">responseType</span>: <span class="string">&#x27;blob&#x27;</span>,</span><br><span class="line">    <span class="attr">headers</span>: &#123;</span><br><span class="line">      <span class="string">&#x27;Content-Type&#x27;</span>: <span class="string">&#x27;application/json&#x27;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">timeout</span>: <span class="number">1000</span> * <span class="number">60</span>,</span><br><span class="line">    <span class="attr">url</span>: url,</span><br><span class="line">    <span class="attr">method</span>: <span class="string">&#x27;get&#x27;</span>,</span><br><span class="line">    <span class="attr">params</span>: &#123;</span><br><span class="line">      <span class="attr">query</span>: qs.stringify(params),</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>utils</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**  </span></span><br><span class="line"><span class="comment"> * 本地保存文件并导出  </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123; Object &#125;</span> </span>Obj 导出文件参数对象  </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123; Blob &#125;</span> </span>file 文件资源  </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123; String &#125;</span> </span>fileName 文件名称(注意：包含后缀)  </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">loacalSaveFile</span>(<span class="params">&#123; file, fileName, option = &#123; type: <span class="string">&#x27;application/vnd.ms-excel&#x27;</span> &#125;&#125;</span>) </span>&#123;  </span><br><span class="line"> <span class="keyword">const</span> ieKit = judgeBrowser(<span class="string">&#x27;ie&#x27;</span>)  </span><br><span class="line"> <span class="keyword">const</span> blobData = <span class="keyword">new</span> Blob([file], option) <span class="comment">// 生成 Blob文件  </span></span><br><span class="line"> <span class="keyword">if</span> (ieKit &amp;&amp; navigator.msSaveBlob) &#123;  </span><br><span class="line"> navigator.msSaveBlob &amp;&amp; navigator.msSaveBlob(blobData, fileName)  </span><br><span class="line"> &#125; <span class="keyword">else</span> &#123;  </span><br><span class="line"> <span class="comment">// 其他  </span></span><br><span class="line"> <span class="keyword">const</span> save_link = <span class="built_in">document</span>.createElement(<span class="string">&#x27;a&#x27;</span>)  </span><br><span class="line"> <span class="keyword">const</span> url = URL.createObjectURL(file) <span class="comment">// 创建url  </span></span><br><span class="line"> save_link.href = url  </span><br><span class="line"> save_link.download = fileName  </span><br><span class="line"> <span class="built_in">document</span>.body.appendChild(save_link)  </span><br><span class="line"> save_link.click()  </span><br><span class="line"> <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line"> <span class="built_in">document</span>.body.removeChild(save_link)  </span><br><span class="line"> <span class="built_in">window</span>.URL.revokeObjectURL(url) <span class="comment">// 回收url  </span></span><br><span class="line"> &#125;, <span class="number">0</span>)  </span><br><span class="line"> &#125;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>调用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> file = <span class="keyword">await</span> exportFile(<span class="built_in">this</span>.exportItem.apiUrl, data)</span><br><span class="line">loacalSaveFile(&#123; file, <span class="attr">fileName</span>: <span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.exportItem.fileName&#125;</span>.xlsx`</span> &#125;)</span><br></pre></td></tr></table></figure>

<h3 id="效果"><a href="#效果" class="headerlink" title="效果"></a>效果</h3><p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110150845496.gif" alt="图片"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog">可爱子</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a target="_blank" rel="noopener" href="https://juejin.cn/post/6953882449235410951">https://juejin.cn/post/6953882449235410951</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">转载自掘金，著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-gitee-blog/tags/Node/">Node</a><a class="post-meta__tags" href="/my-gitee-blog/tags/Vue/">Vue</a><a class="post-meta__tags" href="/my-gitee-blog/tags/%E6%96%87%E4%BB%B6%E5%AF%BC%E5%87%BA/">文件导出</a></div><div class="post_share"><div class="social-share" data-image="/my-gitee-blog/img/NodeJs.jpg" data-sites="wechat,qq"></div><link rel="stylesheet" href="/my-gitee-blog/" media="print" onload="this.media='all'"><script src="/my-gitee-blog/" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-gitee-blog/2021/10/19/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91co.js%20-%20%E8%AE%A9%E5%BC%82%E6%AD%A5%E7%9A%84%E4%BB%A3%E7%A0%81%E5%90%8C%E6%AD%A5%E5%8C%96/"><img class="prev-cover" src="/my-gitee-blog/img/JS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【源码阅读】co.js - 让异步的代码同步化</div></div></a></div><div class="next-post pull-right"><a href="/my-gitee-blog/2021/10/14/%E3%80%90HTTP%E3%80%91%E4%B8%BA%E4%BB%80%E4%B9%88%E8%AF%B4HTTPS%E6%AF%94HTTP%E5%AE%89%E5%85%A8%EF%BC%9FHTTPS%E6%98%AF%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81%E5%AE%89%E5%85%A8%E7%9A%84%EF%BC%9F/"><img class="next-cover" src="/my-gitee-blog/img/Http.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【HTTP】为什么说HTTPS比HTTP安全? HTTPS是如何保证安全的？</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-13</div><div class="title">【NodeJS】NodeJS 服务端请求</div></div></a></div><div><a href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-07</div><div class="title">【NodeJS】NodeJS入门学习</div></div></a></div><div><a href="/my-gitee-blog/2021/10/20/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91koa%E6%B4%8B%E8%91%B1%E6%A8%A1%E5%9E%8B%E4%B8%8Ekoa-compose/" title="【源码阅读】koa洋葱模型与koa-compose"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-20</div><div class="title">【源码阅读】koa洋葱模型与koa-compose</div></div></a></div><div><a href="/my-gitee-blog/2021/10/23/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91update-notifier%20%E6%9B%B4%E6%96%B0%20CLI%20%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E7%9A%84%E9%80%9A%E7%9F%A5-%E8%AF%A6%E8%A7%A3/" title="【源码阅读】update-notifier 更新 CLI 应用程序的通知-详解"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-23</div><div class="title">【源码阅读】update-notifier 更新 CLI 应用程序的通知-详解</div></div></a></div><div><a href="/my-gitee-blog/2021/10/25/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91validate-npm-package-name%20%E9%AA%8C%E8%AF%81%E6%98%AF%E5%90%A6%E6%98%AF%E4%B8%AA%E6%9C%89%E6%95%88%E7%9A%84%20npm%20%E5%8C%85%E5%90%8D/" title="【源码阅读】validate-npm-package-name 验证是否是个有效的 npm 包名"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-25</div><div class="title">【源码阅读】validate-npm-package-name 验证是否是个有效的 npm 包名</div></div></a></div><div><a href="/my-gitee-blog/2021/09/14/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91%E6%89%8B%E6%91%B8%E6%89%8B%E6%95%99%E4%BD%A0%E8%B0%83%E8%AF%95%20vue-devtools%20%EF%BC%8C%E4%BD%A0%E7%A1%AE%E5%AE%9A%E4%B8%8D%E6%9D%A5%E7%9C%8B%E7%9C%8B%E5%98%9B/" title="【源码阅读】手摸手教你调试 vue-devtools ，你确定不来看看嘛"><img class="cover" src="/my-gitee-blog/img/vue%E7%9B%B8%E5%85%B3.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-14</div><div class="title">【源码阅读】手摸手教你调试 vue-devtools ，你确定不来看看嘛</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81NDQ0NS8zMDkxNg=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-gitee-blog/img/avatar.png" onerror="this.onerror=null;this.src='/my-gitee-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Vixcity</div><div class="author-info__description">今日事，今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Vixcity"><i class="fab fa-github"></i><span>我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Vixcity" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2091283625@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是Vixcity目前的小博客</div></div><div class="sticky_layout"><div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden;"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%;"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS 服务端请求"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求">【NodeJS】NodeJS 服务端请求</a><time datetime="2022-07-12T16:00:00.000Z" title="发表于 2022-07-13 00:00:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 1"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1">【Hexo】标签样式列表参考 - 1</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 2"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2">【Hexo】标签样式列表参考 - 2</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 3"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3">【Hexo】标签样式列表参考 - 3</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS入门学习"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习">【NodeJS】NodeJS入门学习</a><time datetime="2022-07-06T16:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-gitee-blog/img/NodeJs.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Vixcity</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div id="running-time"></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-gitee-blog/js/utils.js"></script><script src="/my-gitee-blog/js/main.js"></script><script src="/my-gitee-blog/js/tw_cn.js"></script><script src="/my-gitee-blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/my-gitee-blog/js/newStyle.js"></script><script src="/my-gitee-blog/js/sakura.js"></script><script>setInterval(()=>{let create_time=Math.round(new Date("2021-9-8-00:00:00").getTime()/1000);let timestamp=Math.round((new Date().getTime()+8*60*60*1000)/1000);let second=timestamp-create_time;let time=new Array(0,0,0,0,0);if(second>=365*24*3600){time[0]=parseInt(second/(365*24*3600));second%=365*24*3600}if(second>=24*3600){time[1]=parseInt(second/(24*3600));second%=24*3600}if(second>=3600){time[2]=parseInt(second/3600);second%=3600}if(second>=60){time[3]=parseInt(second/60);second%=60}if(second>0){time[4]=second}currentTimeHtml='小博客已经安全运行 '+time[0]+' 年 '+time[1]+' 天 '+time[2]+' 时 '+time[3]+' 分 '+time[4]+' 秒';var elementById=document.getElementById('running-time');if(elementById){elementById.innerHTML=currentTimeHtml}},1000);</script><script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script><script src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0.5s');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><!-- hexo injector body_end end --><script src="/my-gitee-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>